package com.Fate.service.impl;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import com.Fate.common.CodeMsg;
import com.Fate.common.Constant;
import com.Fate.common.PageInfo;
import com.Fate.common.Result;
import com.Fate.dao.CustomerDao;
import com.Fate.dao.VisitLogDao;
import com.Fate.pojo.Customer;
import com.Fate.pojo.VisitLog;
import com.Fate.service.IcustomerService;

import java.util.Map;

public class CustomerServiceImpl implements IcustomerService {
  private CustomerDao customerDao = new CustomerDao();

  private VisitLogDao visitLogDao = new VisitLogDao();

    @Override
    public Result queryPage(Map<String, String> params, Integer pageNo, Integer pageSize) {
        PageInfo<Customer> data = customerDao.selectPage(params, pageNo, pageSize);

        return new Result(data);
    }

    @Override
    public Result add(String name, String phone, Integer sex, String company, Integer userId) {
        //业务数据校验
        //手机号唯一校验
        Customer customer = customerDao.selectOne(phone);
        if (customer!=null){
            return new Result(CodeMsg.CUSTOMER_PHONE_EXIST_ERROR);
        }

        customerDao.insert(name, phone, sex,company,userId);

        return new Result();
    }

    @Override
    public Result update(String id ,String name, String phone, Integer sex, String company,Integer userId) {

        customerDao.Update(id, name, phone, sex,company);
        return new Result();
    }

    @Override
    public Result deleted(String id) {
        customerDao.deleted(id, Constant.CUSTOMER_DELETE_ED);
        return new Result();
    }

    @Override
    public Result batchSetSaleman(String userId, String[] cids) {
        customerDao.updateSalesman(userId,cids);
        return  new Result();
    }

    @Override
    public Result addVisitLog(String id, String name, Integer userId, String realname,String visitTime,String descp) {
        VisitLog visitLog = new VisitLog();
        visitLog.setUserid(userId);
        visitLog.setRealname(realname);
        visitLog.setCustomerid(Integer.parseInt(id));
        visitLog.setName(name);
        visitLog.setVisittime(DateUtil.parse(visitTime,"yyyy-MM-dd"));
        visitLog.setDescp(descp);
        visitLogDao.insert(visitLog);

        return new Result();
    }

































}
